/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.mapper;

import com.koron.purpose.domain.JobsTemplateHazard;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.koron.ebs.mybatis.EnvSource;

import java.util.LinkedHashMap;
import java.util.List;


public interface JobsTemplateHazardMapper {
	
	@Delete("delete from jobs_template_hazard where template_id=#{id}")
	int delete(@Param("id") String templateId);
	
	@Insert("<script>"
			+ "insert into jobs_template_hazard(id,note,template_id) values"
			+ "<foreach collection ='beanList' item='item' index='index' separator =','>"
			+ "(#{item.id},#{item.note},#{item.templateId}) </foreach>"
			+ "</script>")
	int batchInsert(@Param("beanList") List<JobsTemplateHazard> list);
	
	
	@Select("select * from jobs_template_hazard where template_id=#{id}")
	List<JobsTemplateHazard> queryByTid(@Param("id") String templateId);

	@Select("select DISTINCT note from jobs_template_hazard")
	List<String> queryAllNote();

	@Select("<script>"
			+ "select j.`name` jobName,k.* from jobs_template j LEFT JOIN ("
			+ "select a.template_id as id, "
			+ "<foreach collection ='noteList' item='item' index='index' separator =','>"
			+ "(CASE a.note WHEN #{item} THEN 1 else NULL end) as #{item}"
			+ " </foreach>"
			+ "from jobs_template_hazard as a )k on j.id=k.id "
			+ " where j.id in "
			+ "<foreach collection='idList' item='id' separator=',' open='(' close=')'> #{id} </foreach>"
			+ "order by j.serial "
			+ "</script>")
	List<LinkedHashMap<String,Object>> hazardMatrix(@Param("noteList") List<String> noteList, @Param("idList") List<String> idList);

}
